package com.pacs.util;

import java.security.MessageDigest;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;





public class SecretUtil {
	private static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7','8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	public static String shq1(String value) {
        if (value == null) {
            return null;
        }
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("sha1");
            messageDigest.update(value.getBytes());
            return toHexChars(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
	
	public static byte[] encryptAES(byte[] encryptBytes, String encryptKey){
		try{
			KeyGenerator kgen 	= KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
	        random.setSeed(encryptKey.getBytes());
			kgen.init(128, random);
	
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
			byte[] decryptBytes = cipher.doFinal(encryptBytes);
			return decryptBytes;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static byte[] decryptAES(byte[] encryptBytes, String decryptKey){
		try{
			KeyGenerator kgen 	= KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
	        random.setSeed(decryptKey.getBytes());
			kgen.init(128, random);
	
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
			byte[] decryptBytes = cipher.doFinal(encryptBytes);
			return decryptBytes;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static String base64Encode(byte[] data){
		return Base64.encodeBase64String(data);
	}
	
	public static byte[] base64Decode(String data){
		return Base64.decodeBase64(data);
	}
	
	public static String md5(String txt) {
		byte s[] = md5Bytes(txt);
		if (s == null){
			return null;
		}
		StringBuilder str = new StringBuilder(s.length * 2);
		for (byte value : s) {
			str.append(hexChar[value >>> 4 & 0xf]);
			str.append(hexChar[value & 0xf]);
		}
		return str.toString();
	}
	
	public static String toHexChars(byte[] content){
		StringBuilder str = new StringBuilder(content.length * 2);
		for (byte value : content) {
			str.append(hexChar[value >>> 4 & 0xf]);
			str.append(hexChar[value & 0xf]);
		}
		return str.toString();
	}
	
	public static byte[] md5Bytes(String txt){
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(txt.getBytes("UTF8"));
			return m.digest();
		} catch (Exception e) {
			return null;
		}
	}
	
	public static String md5(byte[] buff){
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(buff);
			buff = m.digest();
			StringBuilder str = new StringBuilder(buff.length * 2);
			for (byte value : buff) {
				str.append(hexChar[value >>> 4 & 0xf]);
				str.append(hexChar[value & 0xf]);
			}
			return str.toString();
		} catch (Exception e) {
			return null;
		}
	}
}
